<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/11/30
 * Time: 9:15
 */
namespace app\admin\controller;
use  think\Db;
class House extends Base {
    public function index(){
        if(request()->isAjax()){
            extract(input());

            $map =array();
            if(isset($key)&&$key!="")
            {
                $map['house_type'] = ['like',"%" . $key . "%"];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_house_type')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_house_type')
                ->alias ('a')
                ->page($page, $limit)
                ->where($map)
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $result['house_type'] = input('param.house_type');
            $aaa = Db::table('up_house_type')->insertGetId($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }


    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(request()->isAjax()){
            $whereMap['house_type_id'] = input('param.id');
            $result['house_type'] = input('param.house_type');
            $aaa = Db::table('up_house_type')->where($whereMap)->update($result);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $where['house_type_id'] = $id;
        $data = Db::table('up_house_type')->where($where)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('param.id');
        $where['house_type_id'] = $id;
        $res = Db::table('up_house_type')->where($where)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}